package com.budwk.app.sqfwV2.generator.publicSecurity.pdf;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowBusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.publicSecurity.PublicSecurityDTO;
import com.budwk.app.sqfwV2.enums.publicSecurity.PublicSecurityFileEnum;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.deepoove.poi.data.PictureRenderData;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-03-25
 * <p>
 * 大营路社会治安 1
 * 【社会治安重点风险场所】合集.docx
 */
public class PublicSecurityPdfGenerator extends BasePdfGenerator {

    /**
     * 判断使用使用这个generator生成pdf
     *
     * @param pdfDTO 创建pdf数据素材
     * @return 结果
     */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        PublicSecurityDTO fieldDTO = (PublicSecurityDTO) pdfDTO.getAbstractCustomerPdfCreateField();
        return true;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        CommonParam customerParamMap = pdfGenerateParam.getCreateUserCommonParam();
        String template = null;
        //2.0新版 根据前端传的数据取对应模板生成数据（取消限制对应区取对应模板，以防后续其他区复用）
        CompleteFlowBusinessParam completeFlowBusinessParam = (CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam();
        if(ObjectUtils.isNotEmpty(completeFlowBusinessParam)){
            Sqfw_formSummary_new formSummaryNew = completeFlowBusinessParam.getFormSummary();
            if(ObjectUtils.isNotEmpty(formSummaryNew)&&StringUtils.isNotBlank(formSummaryNew.getParamData())){
                PublicSecurityDTO publicSecurityDTO = JSONObject.parseObject(formSummaryNew.getParamData(),PublicSecurityDTO.class);
                if(StringUtils.isNotBlank(publicSecurityDTO.getFileMark())){
                    PublicSecurityFileEnum publicSecurityFileEnum =PublicSecurityFileEnum.getPublicSecurityFileByFileMarkAndSuffix(publicSecurityDTO.getFileMark(),"docx");
                    if(ObjectUtils.isNotEmpty(publicSecurityFileEnum)){
                        return publicSecurityFileEnum.getTemplatePath();
                    }
                }
            }
        }
        //由于1.0，2.0是并行的因此老版本走的还是这个
        if ("观山湖区".equals(customerParamMap.getDistrict())) {
            template = PublicSecurityFileEnum.PUBLIC_SECURITY_STANDING_PDF_GSHQ.getTemplatePath();
        } else {
            template = PublicSecurityFileEnum.PUBLIC_SECURITY_STANDING_PDF.getTemplatePath();
        }
        return template;
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        // 在这里对一些数据进行填充 进行复杂的业务代码逻辑
        PublicSecurityDTO fieldDTO = (PublicSecurityDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        if (pdfGenerateParam.isDownTemplate()) {
            buildNullData(paramMap);
            return;
        }

        paramMap.put("fillDate", LocalDateTimeUtil.format(fieldDTO.getInspectionDate(), DatePattern.CHINESE_DATE_PATTERN));
        paramMap.put("mainBusinessScope", fieldDTO.getMainBusinessScope());
        String createUserName = pdfGenerateParam.getCreateUserCommonParam().getCreateUserName();
        String peers = StringUtils.isNotBlank(fieldDTO.getPeersName()) ? fieldDTO.getPeersName() : fieldDTO.getPeers();
        paramMap.put("checkPersons", StringUtils.isNotBlank(peers) ? createUserName + "," + peers : createUserName);

        paramMap.put("checkedUnitLeader", CommonFormOfficeUtils.addWordPicture(fieldDTO.getCheckedUnitLeader(), 2.0F));
        if (StringUtils.isBlank(fieldDTO.getVisitPictures())) {
            paramMap.put("visitPicturess", Lists.newArrayList());

        } else {
            List<Map<String, PictureRenderData>> pictures = CommonFormOfficeUtils.addWordPicture(Lists.newArrayList(fieldDTO.getVisitPictures().split(",")), "visitPictures", 5F);
            paramMap.put("visitPicturess", pictures);
        }
        //案件描述
        if (StringUtils.isBlank(fieldDTO.getCaseType()) || "无".equals(fieldDTO.getCaseType())) {
            paramMap.put("caseTypeDesc", null);
        } else {
            paramMap.put("caseTypeDesc", String.format("%s描述:", fieldDTO.getCaseType()));
        }
    }

    private void buildNullData(Map<String, Object> paramMap) {
        paramMap.put("fillDate", "XXXX年XX月XX日");
        paramMap.put("county", "");
        paramMap.put("mainBusinessScope", "");
        paramMap.put("checkPersons", "");

        paramMap.put("checkedUnitLeader", null);
        paramMap.put("visitPicturess", Lists.newArrayList());
        //案件描述
        paramMap.put("caseTypeDesc", null);
    }
}
